<?php

/* * 
 * 资讯
 */
namespace Api\Model;
use Think\Model;
class AnnouncementModel extends Model {

    //资讯列表
    public function getAnnouncementListByType($type, $pageNum) {

        $map['announcement_type'] = $type;

        $list = $this
            ->field("announcement_id, announcement_image_url, description, announcement_url,
            DATE_FORMAT(create_time, '%m-%d') as create_time, collect_count, announcement_type")
            ->where($map)
            ->order("create_time DESC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        return $list;

    }

    //获取资讯
    public function getAnnouncementById($id) {

        $announcement  = $this
                ->field("announcement_id, DATE_FORMAT(create_time, '%m-%d') as create_time, title, content, 
                announcement_image_url, announcement_video_url, collect_count, comment_count")
                ->where(array('announcement_id' => $id))
                ->find();

        return $announcement;

    }

    //获取资讯评论
    public function getAnnouncementCommentById($id, $pageNum) {

        $map['a.announcement_id'] = $id;
        $map['c.comment_type'] = C("COMMENT_TYPE.ANNOUNCEMENT");

        $commentList = $this->alias('a')
            ->join("LEFT JOIN __COMMENT__ c ON a.announcement_id = c.reference_id")
            ->join("LEFT JOIN __USER__ u ON c.user_id = u.user_id")
            ->field("c.comment_id, u.user_id, u.username, u.head_image_url, DATE_FORMAT(c.create_time,'%m-%d %H:%i') as create_time,
                    c.content, c.root, c.comment_user_id, c.comment_username")
            ->where($map)
            ->order("c.create_time ASC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        return $commentList;

    }

}

